package com.corewillsoft.fireflies.gamelaws;

import com.corewillsoft.fireflies.gameobjects.Firefly;
import com.corewillsoft.fireflies.gameobjects.GameObject;
import com.corewillsoft.fireflies.gameobjects.Portal;

public class PortalFireflyLaw extends BaseLaw {

	@Override
	public void applicate(GameObject gameObject) {
		if ((gameObject instanceof Firefly) && (getParent() instanceof Portal)) {
			processFirefly((Firefly) gameObject, (Portal) getParent());
		}
	}

	private void processFirefly(Firefly firefly, Portal portal) {
		if (firefly.isSaved() || (!firefly.isAlive())) {
			return;
		}

		if (firefly.getRace().equals(portal.getRace())) {
			firefly.setSaved(true);
			float x = getArea().getMinX() + getArea().getWidth() / 2;
			float y = getArea().getMinY() + getArea().getHeight() / 2;
			firefly.setPosition(x, y);
			firefly.setSpeed(0, 0);
		} else {
			firefly.setAlive(false);
		}
	}

	@Override
	public LawType getType() {
		return LawType.PORTAL_FIREFLY;
	}

}
